package com.zw.yzk.kabu.view.activity.imagedetails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.utils.QNImageUtils;
import com.zw.yzk.kabu.view.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.widget.zoonview.PhotoViewAttacher;


/**
 * Created by wei on 2017/3/30.
 */

public class ImageListDetailsActivity extends BaseImageDetailsActivity {

    protected ViewPagerIndicator mIndicator;
    private ImageView defaultBg;
    protected MyViewPagerAdapter adapter;
    private ViewPager viewPager;
    private int currentItem;

    public static void startImageListActivity(Context from, ArrayList<String> picUrl, String fromUrl, Rect fromRect) {
        Intent intent = new Intent(from, ImageListDetailsActivity.class);
        intent.putExtra("url", picUrl);
        intent.putExtra("fromUrl", fromUrl);
        intent.putExtra("fromRect", fromRect);
        from.startActivity(intent);
        //禁用过渡动画
        if (from instanceof Activity) {
            ((Activity) from).overridePendingTransition(0, 0);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_list;
    }

    @Override
    protected String getEnterImageUrl() {
        return getIntent().getStringExtra("fromUrl");
    }

    @Override
    protected Rect getStartRect() {
        return getIntent().getParcelableExtra("fromRect");
    }

    @Override
    protected ImageView getAnimatedImageView() {
        return adapter.getPrimaryItem();
    }

    @Override
    protected void onAnimatedEnd() {
        defaultBg.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        defaultBg = (ImageView) findViewById(R.id.default_bg);

        viewPager = (ViewPager) findViewById(R.id.list);
        List<String> uriList = getIntent().getStringArrayListExtra("url");
        adapter = new MyViewPagerAdapter(uriList);
        adapter.setOnImageClickedListener(new MyViewPagerAdapter.OnImageClickedListener() {
            @Override
            public void onClicked(View v) {
                playExitAnimation(getAnimatedImageView());
            }
        });
        currentItem = uriList.indexOf(getEnterImageUrl());
        viewPager.setAdapter(adapter);
        initIndicator();
    }

    @Override
    protected void playExitAnimation(ImageView existImage) {
        finish();
    }

    public static class MyViewPagerAdapter extends PagerAdapter {
        private Context context;
        private ImageView animationView;
        private int primaryPosition;

        private List<String> uriList;

        MyViewPagerAdapter(List<String> list) {
            this.uriList = list;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            if (context == null) {
                context = container.getContext();
            }
            final ImageView imageView = new ImageView(container.getContext());
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            Glide.with(container.getContext()).load(QNImageUtils.getThumbnail(context, uriList.get(position))).into(new SimpleTarget<GlideDrawable>() {
                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                    if (resource instanceof GlideBitmapDrawable) {
                        imageView.setImageBitmap(((GlideBitmapDrawable) resource).getBitmap());
                        PhotoViewAttacher attacher = new PhotoViewAttacher(imageView);
                        attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                            @Override
                            public void onPhotoTap(View view, float x, float y) {
                                if (listener != null) {
                                    listener.onClicked(view);
                                }
                            }
                        });
                    }
                }
            });
            container.addView(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClicked(v);
                    }
                }
            });
            return imageView;
        }

        @Override
        public int getCount() {
            return uriList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            animationView = (ImageView) object;
            primaryPosition = position;
        }

        ImageView getPrimaryItem() {
            return animationView;
        }

        public interface OnImageClickedListener {
            void onClicked(View v);
        }

        public OnImageClickedListener listener;

        public void setOnImageClickedListener(OnImageClickedListener listener) {
            this.listener = listener;
        }

        public String getCurrentUrl() {
            if (uriList == null) {
                return "";
            }
            if (primaryPosition < uriList.size() && primaryPosition >= 0) {
                return uriList.get(primaryPosition);
            } else {
                return "";
            }
        }

        public int getCurrentPosition() {
            return primaryPosition;
        }
    }

    private void initIndicator() {
        mIndicator = (ViewPagerIndicator) findViewById(R.id.indicator);
        mIndicator.setRadius(4 * getResources().getDisplayMetrics().density);
        mIndicator.setViewPager(viewPager, currentItem);
    }
}
